
/**
 * 类型转换函数
 */
const TypeTransformer = {
    integer: function (v) {return parseInt(v);},
    number: function (v) {return Number(v);},
    string: function (v) {return v.toString();},
    date:function (v) {
        const patt = /^(\d{4})(\d{2})(\d{2})$/;
        let res = patt.exec(v);
        var date = null;
        if (res == null){
            date = v.replace('.', '-');
        }
        else{
            //移除第一个
            res.shift();
            date = res.join('-');
        }
        return date;
    }
};

/**
 * 表头翻译
 */
const TitleTransformer = {
    NAME: {label: '姓名', type:'string'},
    YBID: {label: '保险编号', type:'string'},
    JYDATE: {label: '评估日期', type:'date'},
    PGLX: {label: '评估类型', type:'integer'},
    ZHLX: {label: '照护类型', type:'integer'},
    GRXZ: {label: '个人性质', type:'string'},
    PGDJ: {label: '需求评估等级', type:'integer'},
    JYTOTFY: {label: '交易费用总额', type:'number'},
    GRXJ: {label: '自付现金', type:'integer'},
    TCZF: {label: '基金支付', type:'number'},
    TOTFY: {label: '医保结算范围费用总额', type:'number'},
    FY01: {label: '治疗费', type:'number'},
    FY02: {label: '手术材料费', type:'number'},
    FY04: {label: '化验费', type:'number'},
    FY08: {label: '基础护理费', type:'number'},
    FY09: {label: '其他费', type:'number'},
    ZF: {label: '非医保个人自付', type:'number'},
    FLGRZF: {label: '分类自付', type:'number'},
    DAA: {label: '上报日期', type:'date'},
    YYID: {label: '医院编号', type:'string'},
    SQH: {label: '评估申请号', type:'string'},
    LSH: {label: '交易确认应答返回流水号', type:'string'},
    HLNUM: {label: '护理小时', type:'number'},

};


/**行列型文件对象
 *@param file 输入框返回的文件对象
 * @param encode 文件打开的编码
 */
class RowFile {
    constructor(file, encode) {
        this.file = file;
        this.encode = encode;
        /**
         * @type {Array}
         */
        this.title = null;
        this.data = [];
    }

    getTitle(){
        return this.title;
    }
    getData(){
        return this.data;
    }

    /**
     * @param {object} eventHandler @see FileReader @Input输入框 FileReader事件处理器
     */
    load(eventHandler){
        let reader = new FileReader();
        if(typeof(eventHandler) == 'object'){
            for(let k in eventHandler){
                if(typeof (eventHandler[k]) == 'function'){
                    reader[k] = eventHandler[k];
                }

            }
        }
        reader.onload = (e) => {
            //console.log(e.target.result)
            let result = e.target.result;
            let lines = result.split(/(?:[\r\n]+)/g);
            if (lines[lines.length - 1] == '') {
                lines.pop(); //移除最后一个空字符
            }
            //表头，第一行
            let titles = lines[0].split(/\s+/);
            if (titles[titles.length - 1] == '') {
                titles.pop(); //移除最后一个空字符
            }
            this.title = titles;
            //移除表头
            lines.shift();
            //解码
            this.parse(lines);
            if (eventHandler.onload) {
                eventHandler.onload(this);
            }

        };

        reader.readAsText(this.file, this.encode);
    }

    parse(lines){
        for (let n = 1; n < lines.length; n++) {
            let res = lines[n].split(/\s+/); //列分割
            if (res[res.length - 1] == '') {
                res.pop();
            }
            this.data.push(res);
        }
    }

    /**
     * 使用表头标签翻译表头
     * @param translateTable object|undefined 表头标签表
     * @returns {*}
     */
    getTitleLabel(translateTable){
        if (this.titleLabel) { return this.titleLabel; }
        translateTable = translateTable || TitleTransformer;
        //console.log(translateTable);
        let titleLabel = [];
        for (let n in this.title) {
            if(translateTable[this.title[n]]){
                titleLabel.push(translateTable[this.title[n]].label || this.title[n]);
            }
            else{
                titleLabel.push(this.title[n]);
            }
        }
        this.titleLabel = titleLabel;
        return titleLabel;

    }
}


/**
 * Txt文件类型
 * @see File
 */
class TxtFile extends RowFile{
    constructor(file, encode) {
        super(file, encode);
        this.failedRows = [];
    }

    getFailedRow(){
        return this.failedRows;
    }

    parse(lines) {
        let titles = this.title;

        //从第二行开始读取
        let title_size = titles.length;
        for (let n = 1; n < lines.length; n++) {
            let res = lines[n].split(/\s+/); //列分割
            if (res[res.length - 1] == '') {
                res.pop();
            }
            if (res.length != title_size) {
                this.failedRows.push({line:n, value: res});
                continue;
            }
            else {
                this.data.push(res);
            }
        }
        //格式化
        for (let n in this.title) {
            let title = this.title[n];
            if (!title) {
                continue;
            }
            let col = TitleTransformer[title];
            if (!col) {
                continue;
            }
            let format = col.format;
            if (!format) {
                format = col.type && TypeTransformer[col.type];
            }
            if (!format) {
                continue;
            }
            let data = this.data;
            for (let i in data) {
                let line = data[i];
                line[n] = format(line[n]);
            }
        }

    };
}


export {
    RowFile, TxtFile
}